﻿using Microsoft.Extensions.Primitives;
using System.Collections.Concurrent;
using System.Configuration;

namespace ImClientApi.Utils
{
    /// <summary>
    /// 配置文件
    /// </summary>
    public class ConfigUtils
    {
        /// <summary>
        /// 访问 IConfiguration 实例
        /// </summary>
        public static IConfiguration Configuration;

        private static ConcurrentDictionary<string, object> _cacheDic;
        public static ConcurrentDictionary<string, object> configValues = new ConcurrentDictionary<string, object>();

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetConfigValue(string key)
        {
            if (configValues.ContainsKey(key))
            {
                return configValues[key];
            }

            Configuration = null;
            _cacheDic = new ConcurrentDictionary<string, object>();
            try
            {
                Configuration = new ConfigurationBuilder()
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile("appsettings." + Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") + ".json", optional: true, reloadOnChange: true).Build();

                ChangeToken.OnChange(() => Configuration.GetReloadToken(), delegate
                {
                    if (_cacheDic.Any())
                    {
                        _cacheDic = new ConcurrentDictionary<string, object>();
                    }
                });
            }
            catch (Exception ex)
            {
                Configuration = null;
                return ex.Message;
            }

            if (Configuration == null)
            {
                return string.Empty;
            }

            string[] array = key.Split(':');
            if (array.Length == 2)
            {
                string value = Configuration.GetSection(array[0]).GetSection(array[1]).Value;
                configValues[key] = value;
                return value;
            }

            string value2 = Configuration.GetSection(key).Value;
            configValues[key] = value2;
            return value2;
        }

        /// <summary>
        /// 读取配置文件  可嵌套读取  一级:二级
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigToString(string key)
        {
            try
            {
                object configValue = GetConfigValue(key);
                if (configValue != null)
                {
                    return configValue.ToString();
                }

                return "";
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetConfigToInt(string key)
        {
            var configValue = GetConfigValue(key);
            if (configValue == null)
            {
                return 0;
            }
            try
            {
                return Convert.ToInt32(configValue);
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static long GetConfigToLong(string key)
        {
            var configValue = GetConfigValue(key);
            if (configValue == null)
            {
                return 0;
            }

            try
            {
                return Convert.ToInt64(configValue);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 读取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DateTime GetConfigToDateTime(string key)
        {
            var configValue = GetConfigValue(key);
            if (configValue == null)
            {
                return DateTime.MinValue;
            }

            try
            {
                return Convert.ToDateTime(GetConfigValue(key));
            }
            catch (Exception)
            {
                return DateTime.MinValue;
            }
        }


        /// <summary>
        /// 读取配置 转 bool
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetConfigToBool(string key)
        {
            var configValue = GetConfigValue(key);
            if (configValue == null)
            {
                return false;
            }

            try
            {
                return Convert.ToBoolean(configValue);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 读取配置 转 List
        /// </summary>
        /// <param name="key"></param>
        /// <param name="splitChar"></param>
        /// <returns></returns>
        public static List<string> GetConfigToList(string key, string splitChar = ",")
        {
            var configValue = GetConfigToString(key);
            if (string.IsNullOrWhiteSpace(configValue))
            {
                return new List<string>();
            }

            return configValue.Split(new string[1]
            {
                splitChar
            }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }

        /// <summary>
        /// Aes加密Key
        /// </summary>
        public static string AesKey = GetConfigToString("AesKey");

        /// <summary>
        /// Aes加密Key
        /// </summary>
        public static string AesIv = GetConfigToString("AesIv");
    }
}
